﻿using System;
using System.Collections.Generic;
using System.Text;
using StackExchange.Exceptional;

namespace XW.Utils
{
    /// <summary>
    /// StackExchange.Exceptional 错误扩展
    /// </summary>
    public static class ErrorExtensions
    {
        /// <summary>
        /// 添加body 到自定义内容
        /// </summary>
        /// <param name="error"></param>
        public static void AddBody(this Error error) {
            // 检查自定义内容是否为空
            error.CheckCustomData();
            // 错误内容
            if (!error.CustomData.ContainsKey("body"))
            {
                string body = "";
                try
                {
                    body = CommonHelper.GetBody();
                }
                catch (Exception ee)
                {

                }
                error.CustomData.Add("body", body);
            }
        }
        /// <summary>
        /// 添加用户Id 到自定义内容
        /// </summary>
        /// <param name="error"></param>
        public static void AddUserId(this Error error)
        {
            // 检查自定义内容是否为空
            error.CheckCustomData();
            // 用户Id
            if (!error.CustomData.ContainsKey("cuserid"))
            {
                string userId = "";
                try
                {
                    userId = CommonHelper.GetUserId().ToString();
                }
                catch (Exception ee)
                {

                }
                error.CustomData.Add("cuserid", userId);
            }
        }
        /// <summary>
        /// 检查自定义内容是否为空
        /// </summary>
        /// <param name="error"></param>
        public static void CheckCustomData(this Error error) {
            if (error.CustomData == null)
            {
                error.CustomData = new Dictionary<string, string>();
            }
        }
    }
}
